package org.hdax.dm.service.impl;

import org.elasticsearch.common.recycler.Recycler;
import org.hda.dm.utils.exception.DmException;
import org.hdax.dm.client.LinkUserClient;
import org.hdax.dm.entity.linkuser.LinkUser;
import org.hdax.dm.request.DeleteLinkUserById;
import org.hdax.dm.service.LinkUserService;
import org.hdax.dm.vo.CommonResponse;
import org.hdax.dm.vo.VoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LinkUserServiceImpl implements LinkUserService {

    @Autowired
    private LinkUserClient linkUserClient;

    @Override
    public CommonResponse<LinkUser> queryLinkUser(Long userId) {
        return VoUtil.returnSuccess(linkUserClient.findByUserId(userId));
    }

    @Override
    public CommonResponse<LinkUser> addLinkUser(LinkUser linkUser) {
        return VoUtil.returnSuccess(linkUserClient.addLinkUser(linkUser));
    }

    @Override
    public CommonResponse queryLinkUserByIdCard(String idCard) {
        LinkUser linkUser = linkUserClient.findByIdCard(idCard);
        if (linkUser == null) {
            return VoUtil.returnFail("0000", new DmException("购票人不存在"));
        }
        return VoUtil.returnFail("1009", new DmException("购票人已存在"));
    }

    @Override
    public CommonResponse deleteLinkUserById(DeleteLinkUserById deleteLinkUserById) {
        LinkUser linkUser = linkUserClient.findByIdCard(deleteLinkUserById.getLinkId().toString());
        if (linkUser == null) {
            return VoUtil.returnFail("0000", new DmException("联系人不存在"));
        }
        linkUserClient.deleteLinkUserById(deleteLinkUserById.getLinkId());
        return VoUtil.returnSuccess(null);
    }
}
